<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function _initialize(){
		
	}
	
    public function index(){
    	if(isset($_COOKIE["pro"])&&isset($_COOKIE['city'])){		
    		$address = $_COOKIE["pro"].$_COOKIE['city'];
    	}else{
    		if (isset($_SESSION["res"])){
    		
    			$address = $_SESSION["res"]["pro"].$_SESSION["res"]["city"];
    		}else{
    			
    			$this->getLocation();
    			if(isset($_SESSION["res"])){
    				$address = $_SESSION["res"]["pro"].$_SESSION["res"]["city"];
    			}else{				
    				$getIp=$_SERVER["REMOTE_ADDR"];
    		//echo $getIp;exit;
    				$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=7IZ6fgGEGohCrRKUE9Rj4TSQ&ip={$getIp}&coor=bd09ll");
    				$json = json_decode($content);
    				$address = $json->{'content'}->{'address'};
    			}
    			
    		}
    	}
    	
    	if(isset($address)){
    		$where = "address_str = '$address' and ";
    	}else{
    		
    	}
    	
    	//骞垮憡
    		$banner = M("Banner")->where("display = 'Y'")->select();
    		$this->assign("banner",$banner);
    	//鏍忕洰
    		$sort = M("Sort")->select();
    		$this->assign("sort",$sort);
    		$sort_num = M("Sort")->count();
    		$this->assign("sort_num",$sort_num);
    		
    	//璧勮
    		for($i=0;$i<$sort_num;$i++){
    			$id = $sort[$i]["id"];
    			$news[$id] = M("News")->where($where."sort_id =$id")->select();
    		}
    		
    		$this->assign("news",$news);
    	
		$this->display("index");
    }
    
    public function getAddress(){
    	$getIp=$_SERVER["REMOTE_ADDR"];
    	echo 'IP:',$getIp;
    	echo '<br/>';
    	$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=7IZ6fgGEGohCrRKUE9Rj4TSQ&ip={$getIp}&coor=bd09ll");
    	$json = json_decode($content);
    	
    	
    	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    	echo 'log:',$json->{'content'}->{'point'}->{'x'};//按层级关系提取经度数据
    	echo '<br/>';
    	echo 'lat:',$json->{'content'}->{'point'}->{'y'};//按层级关系提取纬度数据
    	echo '<br/>';
    	print $json->{'content'}->{'address'};//按层级关系提取address数据
    }
    
    public function showL(){
    	$str ="<script src=\"http://api.map.baidu.com/api?v=1.2\" type=\"text/javascript\"></script> 
   <script>
   function setResult(lng, lat){
       $.post(\"/Home/Index/setRes?lng=\"+lng+\"&lat=\"+lat,{code:lng}, function(data){   })        
   }
   function setAA(res){
       $.post(\"/Home/Index/setRes?pro=\"+pro+\"&city=\"+city,{code:pro}, function(data){   })        
   }
           function getLocation(){
                var options={
                    enableHighAccuracy:true, 
                    maximumAge:1000
                }
                if(navigator.geolocation){
                    //浏览器支持geolocation
                    navigator.geolocation.getCurrentPosition(onSuccess,onError,options);
                }else{
                    //浏览器不支持geolocation
                    alert('您的浏览器不支持地理位置定位');
                }
            }

           //成功时
           function onSuccess(position){
                //返回用户位置
               //经度
               var longitude =position.coords.longitude;
                //纬度
               var latitude = position.coords.latitude;
               //setResult(longitude,latitude);
    		   
                
                //根据经纬度获取地理位置，不太准确，获取城市区域还是可以的
		    var map = new BMap.Map(\"allmap\");
		     var point = new BMap.Point(longitude,latitude);
		     var gc = new BMap.Geocoder();
		     gc.getLocation(point, function(rs){
		        var addComp = rs.addressComponents;
		        //alert(addComp.province + \", \" + addComp.city + \", \" + addComp.district + \", \" + addComp.street + \", \" + addComp.streetNumber);
		        setAA(addComp.province,addComp.city);
		     });

           }

           //失败时
           function onError(error){
                switch(error.code){
                    case 1:
                    alert(\"位置服务被拒绝\");
                    break;

                   case 2:
                    alert(\"暂时获取不到位置信息\");
                    break;

                   case 3:
                    alert(\"获取信息超时\");
                    break;

                   case 4:
                     alert(\"未知错误\");
                    break;
                }

           }

           window.onload=getLocation;
          
    </script>";
    	echo $str;
    	
    	dump($_SESSION);
    }
    public function setRes(){
    	
	    	$_COOKIE["res"]["lat"] = $_REQUEST["lat"];
	    	$_COOKIE["res"]["lng"] = $_REQUEST["lng"];
	 
    }
    
    public function test(){
    	if ($this->isMobile()){
    		echo "yes";
    	}else{
    		echo "no";
    	}
    	
    	dump($_SERVER);
    	exit;
//     	$this->getLocation();
// 		dump($_COOKIE);
// 		dump($_SESSION);
		$address = $_REQUEST["pro"].$_REQUEST["city"];
		$where = "address_str = '$address' and ";
    	$banner = M("Banner")->where("display = 'Y'")->select();
    	$this->assign("banner",$banner);
    	//鏍忕洰
    	$sort = M("Sort")->select();
    	$this->assign("sort",$sort);
    	$sort_num = M("Sort")->count();
    	$this->assign("sort_num",$sort_num);
    	
    	//璧勮
    	for($i=0;$i<$sort_num;$i++){
    		$id = $sort[$i]["id"];
    		$news[$id] = M("News")->where($where."sort_id =$id")->select();
    	}
    	
    	$this->assign("news",$news);
		$this->display();
    
    }
    public function getdata(){
    	$abcd = $this->getLocation();
    	echo $abcd;
    }
    public function getLocation(){
    	$this->display("getLocation");
    }

    public function ajaxRe(){
    	$pro = $_REQUEST["pro"];
    	$city = $_REQUEST["city"];
    	setcookie("pro",$pro);
    	setcookie("city",$city);
    	$_SESSION["res"]["pro"] = $_REQUEST["pro"];
    	$_SESSION["res"]["city"] = $_REQUEST["city"];
		//echo json_encode($_REQUEST["pro"]);
		
    }
    
    function isMobile(){
    
    	$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    	$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
    
    	$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
    	$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
    	$found_mobile=$this->CheckSubstrs($mobile_os_list,$useragent_commentsblock) || $this->CheckSubstrs($mobile_token_list,$useragent);
    	if ($found_mobile){
    		return true;
    	}else{
    		return false;
    	}
    
    }
    
    function CheckSubstrs($substrs,$text){
    	foreach($substrs as $substr)
    		if(false!==strpos($text,$substr)){
    		return true;
    	}
    	return false;
    }
    

}